﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LabaCS3
{
    public partial class Main : Form
    {

        public Main()
        {
            InitializeComponent();
        }

        public ArrayList bookForm;

        private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Add myform = new Add();
            DialogResult dr = myform.ShowDialog();
            if (dr == DialogResult.OK)
            {
                if (bookForm == null)
                {
                    bookForm = new ArrayList();

                }

                int cost = Convert.ToInt32(myform.Cost);
                Book cbook = new Book(myform.Title, myform.Author, cost);
                myform.fillBook(cbook); //заполняем книгу главами
                bookForm.Add(cbook); //добавляем книгу с главами
                String s = cbook.Title + "," + cbook.AuthorName + "," + cbook.Cost.ToString();
                listBox1.Items.Add(s);

            }

        }


        private void просмотретьToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }



        private void оПрограммеToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            About myform = new About();
            myform.ShowDialog(this);

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }


        private void Main_Load(object sender, EventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {


        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {


        }



        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }

        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                int deleteIndex = listBox1.SelectedIndex;
                listBox1.Items.RemoveAt(deleteIndex);
                listBox2.Items.Clear();
                bookForm.RemoveAt(deleteIndex);

            }
        }

        private void textBox2_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int cIndex = listBox1.SelectedIndex;
            if (cIndex >= 0)
            {
                Book cbook = (Book)bookForm[cIndex];
                listBox2.Items.Clear();
                foreach (Chapter cg in cbook)
                {
                    listBox2.Items.Add(cg.chapters);

                }
            }
        }

        private void listView1_SelectedIndexChanged_2(object sender, EventArgs e)
        {


        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                int deleteIndex = listBox1.SelectedIndex;
                listBox1.Items.RemoveAt(deleteIndex);
                listBox2.Items.Clear();
                bookForm.RemoveAt(deleteIndex);

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Add myform = new Add();
            DialogResult dr = myform.ShowDialog();
            if (dr == DialogResult.OK)
            {
                if (bookForm == null)
                {
                    bookForm = new ArrayList();

                }

                int cost = Convert.ToInt32(myform.Cost);
                Book cbook = new Book(myform.Title, myform.Author, cost);
                myform.fillBook(cbook); //заполняем книгу главами
                bookForm.Add(cbook); //добавляем книгу с главами
                String s = cbook.Title + "," + cbook.AuthorName + "," + cbook.Cost.ToString();
                listBox1.Items.Add(s);

            }



        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                Add myform = new Add();
                int changeIndex = listBox1.SelectedIndex;

                Book cbook = (Book)bookForm[changeIndex];

                myform.Title = cbook.Title;
                myform.Author = cbook.AuthorName;
                myform.Cost = cbook.Cost;

                myform.GetChapterFromBook(cbook);
                DialogResult dr = myform.ShowDialog();


                if (dr == DialogResult.OK)
                {

                    int cost = Convert.ToInt32(myform.Cost);
                    cbook = new Book(myform.Title, myform.Author, cost);
                    myform.fillBook(cbook);
                    bookForm[changeIndex] = cbook;

                    String s = cbook.Title + "," + cbook.AuthorName + "," + cbook.Cost.ToString();

                    listBox1.Items.Insert(changeIndex, s);
                    listBox1.Items.RemoveAt(changeIndex + 1);

                }
            }

        }

        private void редактироватьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                Add myform = new Add();
                int changeIndex = listBox1.SelectedIndex;

                Book cbook = (Book)bookForm[changeIndex];

                myform.Title = cbook.Title;
                myform.Author = cbook.AuthorName;
                myform.Cost = cbook.Cost;

                myform.GetChapterFromBook(cbook);
                DialogResult dr = myform.ShowDialog();


                if (dr == DialogResult.OK)
                {

                    int cost = Convert.ToInt32(myform.Cost);
                    cbook = new Book(myform.Title, myform.Author, cost);
                    myform.fillBook(cbook);
                    bookForm[changeIndex] = cbook;

                    String s = cbook.Title + "," + cbook.AuthorName + "," + cbook.Cost.ToString();

                    listBox1.Items.Insert(changeIndex, s);
                    listBox1.Items.RemoveAt(changeIndex + 1);

                }
            }
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("Вы хотите закрыть приложение?", "Лаба 3",
             MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                Application.Exit();
            }




        }

    }


}





